package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ICustomerRequireService;
import com.mdd.admin.validate.CustomerRequireCreateValidate;
import com.mdd.admin.validate.CustomerRequireUpdateValidate;
import com.mdd.admin.validate.CustomerRequireSearchValidate;
import com.mdd.admin.vo.CustomerRequireListedVo;
import com.mdd.admin.vo.CustomerRequireDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.CustomerRequire;
import com.mdd.common.mapper.CustomerRequireMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户要求实现类
 * @author LikeAdmin
 */
@Service
public class CustomerRequireServiceImpl implements ICustomerRequireService {
        
    @Resource
    CustomerRequireMapper customerRequireMapper;

    /**
     * 客户要求列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CustomerRequireListedVo>
     */
    @Override
    public PageResult<CustomerRequireListedVo> list(PageValidate pageValidate, CustomerRequireSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<CustomerRequire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        customerRequireMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:caseType@case_type:str",
        });

        IPage<CustomerRequire> iPage = customerRequireMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CustomerRequireListedVo> list = new LinkedList<>();
        for(CustomerRequire item : iPage.getRecords()) {
            CustomerRequireListedVo vo = new CustomerRequireListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户要求详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return CustomerRequire
     */
    @Override
    public CustomerRequireDetailVo detail(Integer id) {
        CustomerRequire model = customerRequireMapper.selectOne(
                new QueryWrapper<CustomerRequire>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CustomerRequireDetailVo vo = new CustomerRequireDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户要求新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(CustomerRequireCreateValidate createValidate) {
        CustomerRequire model = new CustomerRequire();
        model.setCaseType(createValidate.getCaseType());
        model.setRequireType(createValidate.getRequireType());
        model.setFileName(createValidate.getFileName());
        model.setTitle(createValidate.getTitle());
        model.setDescription(createValidate.getDescription());
        model.setIsValid(createValidate.getIsValid());
        model.setUpdateUserName(createValidate.getUpdateUserName());
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        customerRequireMapper.insert(model);
    }

    /**
     * 客户要求编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(CustomerRequireUpdateValidate updateValidate) {
        CustomerRequire model = customerRequireMapper.selectOne(
                new QueryWrapper<CustomerRequire>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCaseType(updateValidate.getCaseType());
        model.setRequireType(updateValidate.getRequireType());
        model.setFileName(updateValidate.getFileName());
        model.setTitle(updateValidate.getTitle());
        model.setDescription(updateValidate.getDescription());
        model.setIsValid(updateValidate.getIsValid());
        model.setUpdateUserName(updateValidate.getUpdateUserName());
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        customerRequireMapper.updateById(model);
    }

    /**
     * 客户要求删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        CustomerRequire model = customerRequireMapper.selectOne(
                new QueryWrapper<CustomerRequire>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        customerRequireMapper.updateById(model);
    }

}
